package com.yanfan.energy.controller;

import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.energy.domain.DeviceThingsEnergy;
import com.yanfan.energy.domain.statist.CompanyBuildArea;
import com.yanfan.energy.domain.statist.CompanyBuildAreaEntity;
import com.yanfan.energy.service.BuildService;
import com.yanfan.energy.service.DeviceThingsEnergyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author huan
 * @value 公司楼栋楼层区域
 * @ClassName 2023年07月27日 17:35:00
 * @Version 1.0
 **/
@RestController
@Api(tags = "能源位置相关配置，设备与区域的关联关系")
@RequestMapping("/energyBuild")
public class BuildController {


    @Resource
    private BuildService buildService;

    @Resource
    private DeviceThingsEnergyService deviceThingsEnergyService;

    /**
     * 查询所有公司、楼栋、楼层 、区域
     *
     * @return 查询结果
     */
//    @ApiOperation("查询所有公司、楼栋、楼层 、区域")
    @PostMapping("/deviceLocation")
    public AjaxResult deviceLocation(@RequestBody CompanyBuildArea companyBuildArea) {
        if (companyBuildArea.getPageNum() != null && companyBuildArea.getPageSize() != null) {
            PageHelper.startPage(companyBuildArea.getPageNum(), companyBuildArea.getPageSize());
        }
        PageInfo<CompanyBuildArea> pageInfo = new PageInfo<>(buildService.deviceLocation(companyBuildArea));
        return AjaxResult.success(pageInfo);
    }

    /**
     * 查询所有公司、楼栋、楼层 、区域
     *
     * @return 查询结果
     */
//    @ApiOperation("新增（所有公司、楼栋、楼层 、区域）")
    @PostMapping("/deviceLocationAdd")
    public AjaxResult deviceLocationAdd(@RequestBody CompanyBuildArea companyBuildArea) {
        buildService.deviceLocationAdd(companyBuildArea);
        return AjaxResult.success();
    }

    /**
     * 查询所有公司、楼栋、楼层 、区域
     *
     * @return 查询结果
     */
//    @ApiOperation("查询所有公司、楼栋、楼层 、区域")
    @PostMapping("/deviceLocationUpdate")
    public AjaxResult deviceLocationUpdate(@RequestBody CompanyBuildArea companyBuildArea) {
        buildService.deviceLocationUpdate(companyBuildArea);
        return AjaxResult.success();
    }

    /**
     * 查询所有公司、楼栋、楼层 、区域
     *
     * @return 查询结果
     */
//    @ApiOperation("查询所有公司、楼栋、楼层 、区域")
    @PostMapping("/deviceLocationDetail")
    public AjaxResult deviceLocationDetail(@RequestBody CompanyBuildArea companyBuildArea) {
        return AjaxResult.success(buildService.deviceLocationDetail(companyBuildArea));
    }


    /**
     * 查询所有公司、楼栋、楼层 、区域
     *
     * @return 查询结果
     */
    @ApiOperation("查询所有公司、楼栋、楼层 、区域")
    @PostMapping("/deviceLocationDelete")
    public AjaxResult deviceLocationDelete(@RequestBody CompanyBuildArea companyBuildArea) {
        buildService.deviceLocationDelete(companyBuildArea);
        return AjaxResult.success();
    }


    /**
     * 查询所有公司、楼栋、楼层 、区域
     *
     * @return 查询结果
     */
    @ApiOperation("查询所有公司、楼栋、楼层 、区域")
    @GetMapping("/deviceLocationDeptTree")
    public AjaxResult deviceLocationDeptTree() {
        return AjaxResult.success(buildService.deviceLocationDeptTree());
    }


    /**
     * 查询  楼栋楼层区域的关联关系
     */
    @GetMapping("/list")
    @ApiOperation("查询 公司楼栋楼层区域关联关系 列表")
    public AjaxResult list(@ApiParam(name = "status", value = "状态") @RequestParam(required = false) Integer status, @ApiParam(name = "name", value = "名称") @RequestParam(required = false) String name) {
        return AjaxResult.success(buildService.selectCompanyBuildAreaList(status, name));
    }


    /**
     * 新增
     */
    @PostMapping("/add")
    @ApiOperation("新增 公司楼栋楼层区域")
    public AjaxResult add(@RequestBody CompanyBuildAreaEntity entity) {
        buildService.addCompanyBuildArea(entity);
        return AjaxResult.success();
    }

    /**
     * 新增
     */
    @PostMapping("/update")
    @ApiOperation("更新 公司楼栋楼层区域")
    public AjaxResult update(@RequestBody CompanyBuildAreaEntity entity) {
        buildService.updateCompanyBuildArea(entity);
        return AjaxResult.success();
    }

    /**
     * 新增
     */
    @ApiOperation("删除 公司楼栋楼层区域")
    @GetMapping("/delete")
    public AjaxResult delete(CompanyBuildAreaEntity entity) {
        buildService.deleteCompanyBuildArea(entity);
        return AjaxResult.success();
    }

    /**
     * 详情
     */
    @ApiOperation("详情 公司楼栋楼层区域")
    @GetMapping("/detail")
    public AjaxResult detail(CompanyBuildAreaEntity entity) {
        return AjaxResult.success(buildService.detailCompanyBuildArea(entity));
    }

    /**
     * 导出
     */
    @ApiOperation("公司楼栋楼层区域 导出")
    @GetMapping("/export")
    public AjaxResult export(HttpServletResponse response, @ApiParam(name = "status", value = "状态") @RequestParam(required = false) Integer status, @ApiParam(name = "name", value = "名称") @RequestParam(required = false) String name) throws IOException {
        buildService.export(response, status, name);
        return AjaxResult.success();
    }


    @ApiOperation("根据楼栋Ids查楼层")
    @GetMapping("/findFloorsByBuilds")
    public AjaxResult findFloorsByBuilds(@ApiParam(name = "buildIds", value = "楼栋ID集合") @RequestParam(required = false) Integer[] buildIds) {
        return AjaxResult.success(buildService.findFloorsByBuilds(buildIds));
    }


    @ApiOperation("根据楼层Ids查区域")
    @GetMapping("/findAreaByFloors")
    public AjaxResult findAreaByFloors(@ApiParam(name = "floorIds", value = "楼栋ID集合") @RequestParam(required = false) Integer[] floorIds) {
        return AjaxResult.success(buildService.findAreaByFloors(floorIds));
    }


    /**
     * 查询区域下对应的设备
     *
     * @return 查询结果
     */
    @ApiOperation("查询区域下对应的设备")
    @GetMapping("/findBindDeviceEnergy")
    public AjaxResult findBindDeviceEnergy(@ApiParam(name = "type", value = "选择类型") @RequestParam(required = false) Integer type, @ApiParam(name = "id", value = "车间id") @RequestParam Integer id, @ApiParam(name = "deviceName", value = "设备名称") @RequestParam(required = false) String deviceName, @ApiParam(name = "pageNum", value = "当前页") @RequestParam Integer pageNum, @ApiParam(name = "pageSize", value = "页条数") @RequestParam Integer pageSize) {
        return deviceThingsEnergyService.findBindDeviceEnergy(type, id, deviceName, pageNum, pageSize);
    }


    @ApiOperation("新增设备区域绑定")
    @PostMapping("/addBindDeviceEnergy")
    public AjaxResult addBindDeviceEnergy(@ApiParam(name = "deviceThingsEnergy", value = "设备区域绑定对象") @RequestBody DeviceThingsEnergy deviceThingsEnergy) {
        return deviceThingsEnergyService.addBindDeviceEnergy(deviceThingsEnergy);
    }

    @ApiOperation("修改设备区域绑定")
    @PostMapping("/updateBindDeviceEnergy")
    public AjaxResult updateBindDeviceEnergy(@ApiParam(name = "deviceThingsEnergy", value = "设备区域绑定对象") @RequestBody DeviceThingsEnergy deviceThingsEnergy) {
        return deviceThingsEnergyService.updateBindDeviceEnergy(deviceThingsEnergy);
    }

    @ApiOperation("删除设备绑定能源")
    @DeleteMapping("/deleteBindDeviceEnergy")
    public AjaxResult deleteBindDeviceEnergy(@ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Integer workshopId, @ApiParam(name = "type", value = "能源类型") @RequestParam Integer type, @ApiParam(name = "id", value = "id") @RequestParam Integer id, @ApiParam(name = "thingsId", value = "物模型id") @RequestParam Integer thingsId) {
        return deviceThingsEnergyService.deleteBindDeviceEnergy(workshopId, type, id, thingsId);
    }


    @ApiOperation("根据设备id查询物模型下拉孔")
    @GetMapping("/findIotThingsByDeviceId")
    public AjaxResult findIotThingsByDeviceId(@ApiParam(name = "deviceId", value = "设备id") @RequestParam(required = false) Integer deviceId, @ApiParam(name = "name", value = "物模型名称") @RequestParam(required = false) String name,@ApiParam(name = "serialNumber", value = "设备编号") @RequestParam(required = false) String serialNumber) {
        return AjaxResult.success(deviceThingsEnergyService.findIotThingsByDeviceId(deviceId, name,serialNumber));
    }

    @ApiOperation("根据设备id查询物模型下拉孔Cop")
    @GetMapping("/findIotThingsByDeviceIdCop")
    public AjaxResult findIotThingsByDeviceIdCop(@ApiParam(name = "deviceId", value = "设备id") @RequestParam(required = false) Integer deviceId, @ApiParam(name = "name", value = "物模型名称") @RequestParam(required = false) String name,@ApiParam(name = "serialNumber", value = "设备编号") @RequestParam(required = false) String serialNumber) {
        return AjaxResult.success(deviceThingsEnergyService.findIotThingsByDeviceIdCop(deviceId, name,serialNumber));
    }


}
